using System;
//linq表达式
using System.Linq;
//简化编写并发和异步代码的工作的类型
using System.Threading.Tasks;
//定义泛型集合的接口和类，可允许用户创建强类型集合
using System.Collections.Generic;
//定义微软旗下跨平台实体框架
using Microsoft.EntityFrameworkCore;
//该项目下的实体
using MyApi.Entity;
//该项目下的Db
using MyApi.Db;
namespace MyApi.Repository
{
    public class EfRepository<T> : IRepository<T> where T : BaseEntity
    {
        //初始化数据库上下文实例化对象
        private MyApiDb _db;

        //构造函数
        public EfRepository(MyApiDb db)
        {
            _db = db;
        }
        //定义一个字段，用于Entity属性
        private DbSet<T> _entity;

        // 一个访问受限制的属性，只有访问器，总是返回一个代表T类型的表对象
        protected DbSet<T> Entity
        {
            get
            {
                if (_entity == null)
                {
                    _entity = _db.Set<T>();
                }
                return _entity;
            }
        }


        //查询T类型的表
        public IQueryable<T> Table
        {
            get
            {
                return Entity.AsQueryable<T>();
            }
        }

        public void Delete(int id)
        {
            var t = Entity.Where(x => x.Id == id).FirstOrDefault();
            Entity.Remove(t);
            _db.SaveChanges();
        }

        //查找指定Id数据
        public T GetById(int id)
        {
            var t = Entity.Where(x => x.Id == id).FirstOrDefault();
            return t;
        }

        //插入
        public void Insert(T entity)
        {
            if (entity == null)
            {
                throw new System.NullReferenceException();
            }
            entity.IsActived = true;
            entity.IsDelete = false;
            entity.CreatedTime = DateTime.Now;
            entity.UpdatedTime = DateTime.Now;
            entity.DisplayOrder = 0;
            Entity.Add(entity);
            _db.SaveChanges();
        }

        //异步插入
        public async Task InsertAsync(T entity)
        {
            if (entity == null)
            {
                throw new System.NullReferenceException();
            }
            entity.IsActived = true;
            entity.IsDelete = false;
            entity.CreatedTime = DateTime.Now;
            entity.UpdatedTime = DateTime.Now;
            entity.DisplayOrder = 0;
            await Entity.AddAsync(entity);
            await _db.SaveChangesAsync();
        }

        //异步批量插入
        public async Task InsertAsyncBulk(IEnumerable<T> entites)
        {
            foreach (var entity in entites)
            {
                entity.IsActived = true;
                entity.IsDelete = false;
                entity.CreatedTime = DateTime.Now;
                entity.UpdatedTime = DateTime.Now;
                entity.DisplayOrder = 0;
            }
            await Entity.AddRangeAsync(entites);
            await _db.SaveChangesAsync();
        }

        //批量插入
        public void InsertBulk(IEnumerable<T> entites)
        {
            foreach (var entity in entites)
            {
                entity.IsActived = true;
                entity.IsDelete = false;
                entity.CreatedTime = DateTime.Now;
                entity.UpdatedTime = DateTime.Now;
                entity.DisplayOrder = 0;
            }
            Entity.AddRangeAsync(entites);
            _db.SaveChangesAsync();
        }
        //修改
        public void Update(T entity)
        {
            if(entity==null){
                throw new System.NullReferenceException();
            }
            entity.UpdatedTime=DateTime.Now;
            _db.SaveChanges();
        }

        //批量修改
        public void UpdateBulk(IEnumerable<T> entites)
        {
            foreach (var entity in entites)
            {
                entity.UpdatedTime=DateTime.Now;
            }
            _db.SaveChanges();
        }
    }
}